home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-05 / lanutsrc.zip / WAITFOR.C < prev    next >
Text File  |  1991-03-13  |  7KB  |  249 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dos.h>
  5. #include <conio.h>
  6. #include <time.h>
  7.  
  8. #include "lantasti.h"
  9.  
  10. #define DELIM "+, "
  11.             
  12. #define LIST_SIZE 50
  13. #define NAME_SIZE 20
  14. typedef struct LIST {
  15.     int num_servers,time;
  16.     int  available[LIST_SIZE];
  17.     char server[LIST_SIZE][NAME_SIZE];
  18.       } LIST;
  19.  
  20. /* command line options */
  21. #define NUM_OPTIONS 2
  22. char *options[NUM_OPTIONS] = {
  23.     "TIME",
  24.     "HELP"
  25.   };
  26.            
  27. /***********************************************************************
  28.  Delete leading and trailing blanks.
  29. ***********************************************************************/
  30. void dltb(string)
  31.   char *string;
  32. {
  33.   register int i;
  34.  
  35.   i = strspn(string," ");
  36.   if (i) strcpy(string,string + i);
  37.  
  38.   i = strlen(string) - 1;
  39.   while (i && (string[i] == ' ')) {
  40.     string[i--] = '\0';
  41.   }
  42. }
  43. /* get_active_servers ********************************************************************
  44. Gets a list of the active servers.
  45. *****************************************************************************/
  46. void get_active_servers(list)
  47.   LIST *list;
  48. {
  49.   char buffer[17];
  50.   int index,result;
  51.   
  52.   index = 0;
  53. /* pull in the name of a server */
  54.   while ((result = get_active_server(buffer,index++))) {
  55.     sprintf(list->server[list->num_servers++],"\\\\%s",buffer);
  56. /* only allow a reasonable number of servers */
  57.     if (list->num_servers >= LIST_SIZE) {
  58.       puts("Warning: Too many servers.  Only the first 50 can be used.");
  59.       break;
  60.     }
  61.   }
  62. }
  63.  
  64. /* get_available_servers ********************************************************************
  65.  
  66. *****************************************************************************/
  67. void get_available_servers(list)
  68.   LIST *list;
  69. {
  70.   char buffer[17];
  71.   int index,result;
  72.   
  73.   index = 0;
  74.   result = get_active_server(buffer,index);
  75.   while (result) {
  76.     dltb(buffer);
  77.     strcpy(list->server[list->num_servers++],buffer);
  78.     if (list->num_servers >= LIST_SIZE) {
  79.       puts("Warning: Too many servers.  Only the first 50 can be used.");
  80.       break;
  81.     }
  82.     result = get_active_server(buffer,++index);
  83.   }
  84.   
  85.   result = get_inactive_server(buffer,index);
  86.   while (result) {
  87.     dltb(buffer);
  88.     strcpy(list->server[list->num_servers++],buffer);
  89.     if (list->num_servers >= LIST_SIZE) {
  90.       puts("Warning: Too many servers.  Only the first 50 can be used.");
  91.       break;
  92.     }
  93.     result = get_inactive_server(buffer,++index);
  94.   }
  95. }
  96.  
  97. /* process_server_list ********************************************************************
  98.  
  99. *****************************************************************************/
  100. void process_server_list(string,list)
  101.   char *string;
  102.   LIST *list;
  103. {
  104.   char *ptr;
  105.   
  106.   ptr = strtok(string,DELIM);
  107.   
  108.   while (ptr !=NULL) {
  109.     list->available[list->num_servers] = FALSE;  
  110.     strcpy(list->server[list->num_servers++],ptr);
  111.  
  112.     if (list->num_servers >= LIST_SIZE ) {
  113.       puts("Warning: Too many servers.  Only the first 50 will be used.");
  114.       break;
  115.     }
  116.     ptr = strtok(NULL," ,");
  117.   }
  118. }
  119.             
  120. /* process_option ********************************************************************
  121.  
  122. *****************************************************************************/
  123. void process_option(string,list)
  124.   char *string;
  125.   LIST *list;
  126. {
  127.   char *ptr;
  128.   int i,tm; 
  129.   
  130.   ptr = strtok(string,"=:");
  131.   
  132.   for (i = 0; i < NUM_OPTIONS; i++) {
  133.     if (!strcmpi(ptr,options[i])) break;
  134.   }
  135.   
  136.   if (i >= NUM_OPTIONS) {
  137.     printf("Whoops! %s isn't a valid command line option.\n",ptr);  
  138.     return;
  139.   }
  140.  
  141.   ptr = strtok(NULL," =");
  142.   
  143.   switch (i)  {
  144.     case 0:   /* time */
  145.       tm = atoi(ptr);
  146.       if ((tm >= 0) && (tm <= 999)) list->time = tm;
  147.       else {
  148.         printf("Whoops! %d isn't a valid timeout value (0..999 seconds).\n",tm);  
  149.         puts("Assuming default value (no timeout)");
  150.         list->time = 0;
  151.       }
  152.       break;
  153.     case 1:   /* help */
  154.       puts("WAITFOR utility for LANtastic -- Copyright 1989 by SoftMagic, Inc.");
  155.       puts("All rights reserved.  LANtastic is a trademark of Artisoft, Inc.\n");
  156.       puts("Usage: WAITFOR <server list> [/OPTIONS]");
  157.       puts("Pauses until all the given servers are on line or until the");
  158.       puts("user presses escape or the optional timeout expires.\n");
  159.       puts("Available options are:");
  160.       puts("/TIME         - specify timeout value in seconds");
  161.       puts("/HELP         - display this documentation");
  162.       exit(0);
  163.       break;
  164.   }
  165. }
  166.  
  167. /* scan_command_line ********************************************************************
  168.  
  169. *****************************************************************************/
  170. scan_command_line(argc,argv,list)
  171.   int argc;
  172.   char *argv[];
  173.   LIST *list;
  174. {
  175.   int i,state;
  176.   char s_buffer[128];
  177.   
  178.   list->num_servers = 0;
  179.   
  180.   for (i = 1;i < argc; i++) {
  181.     strupr(argv[i]);
  182.     if (argv[i][0] == '/') {
  183.       process_option(&argv[i][1],list);
  184.       continue;
  185.     }
  186.     else process_server_list(argv[i],list);
  187.   }
  188. }
  189.  
  190. /* waitfor ********************************************************************
  191.  
  192. *****************************************************************************/
  193. int waitfor(list)
  194.   LIST *list;
  195. {
  196.   LIST avail;
  197.   int i,j,result;
  198.   unsigned long st_time,curr_time;
  199.     
  200.   result = FALSE;
  201.   
  202.   time(&st_time);
  203.   curr_time = st_time;
  204.  
  205. /* while not all servers are available */  
  206.   while (!result) {
  207.     time(&curr_time);
  208.     if (list->time < (curr_time - st_time)) break;
  209.  
  210.     if (kbhit()) if (getch() == 27) break;
  211.   
  212.     result = TRUE;
  213.  
  214.     avail.num_servers = 0;
  215.     get_available_servers(&avail);
  216.  
  217.     for (i = 0;i < list->num_servers; i++) {    
  218.       for (j = 0; j < avail.num_servers; j++) { 
  219.         if (!strcmp(list->server[i],avail.server[j])) 
  220.           list->available[i] = TRUE;
  221.       }
  222.       result = result && list->available[i];
  223.     }
  224.     time(&curr_time);
  225.     if (list->time < (curr_time - st_time)) break;
  226.   }        
  227.   return(!result);   /* return 0 if ok, 1 if error */
  228. }    
  229.  
  230. /* main ********************************************************************
  231.  
  232. *****************************************************************************/
  233. int main(argc,argv)
  234.   int argc;
  235.   char *argv[];
  236. {
  237.   LIST list;
  238.   
  239.   list.time = 32767;            /* default wait time */
  240.  
  241. #ifdef SHAREWARE
  242.   puts("WAITFOR utility for LANtastic -- Copyright 1989 by SoftMagic, Inc.");
  243.   puts("All rights reserved.  Thanks for trying this unregistered Shareware edition!\n");
  244. #endif  
  245.   
  246.   scan_command_line(argc,argv,&list);  
  247.   
  248.   return(waitfor(&list));
  249. }